package top.maof.book.controller;

import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.maof.book.model.Comment;
import top.maof.book.model.Role;
import top.maof.book.result.ApiResponse;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.result.Result;
import top.maof.book.service.CommentService;
import top.maof.book.utils.JWTUtils;
import top.maof.book.vo.CommentVo;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/comment")
@ResponseBody
@Slf4j
public class CommentController {
    @Autowired
    CommentService commentService;

    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse create(@RequestBody Comment comment) {
        comment.setContent(EmojiParser.parseToAliases(comment.getContent()));
        return ApiResponse.success(commentService.save(comment));
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    public ApiResponse deleteById(@PathVariable Integer id) {
        commentService.removeById(id);
        return ApiResponse.success(null);
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    public ApiResponse deleteByIdInBatch(@RequestParam String ids) {
        String[] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Integer.valueOf(id));
        }
        commentService.removeBatchByIds(idList);
        return ApiResponse.success(null);
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    public ApiResponse updateById(@RequestBody Comment comment) {
        comment.setContent(EmojiParser.parseToAliases(comment.getContent()));
        commentService.updateById(comment);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public ApiResponse<PageData<Comment>> findPage(@RequestBody PageWrap<Comment> pageWrap) {
        return ApiResponse.success(commentService.findPage(pageWrap));
    }

    // 发表评论
    @ResponseBody
    @RequestMapping(value = "publish", method = RequestMethod.GET)
    public Result publish(Comment c, @RequestHeader("token") String token) {
        c.setUserId(JWTUtils.getUserId(token));
        c.setTime(System.currentTimeMillis());
        c.setId(null);
        c.setContent(EmojiParser.parseToAliases(c.getContent()));
        int add = commentService.insert(c);
        if (add > 0)
            return Result.success();
        return Result.response_400();
    }

    // 获取书评
    @ResponseBody
    @GetMapping("list")
    public Result list(Integer bookId, Integer page, Integer pageSize) {
        return Result.success(commentService.list(bookId, page, pageSize));
    }


    @ResponseBody
    @GetMapping("get")
    public Result get(int id) {
        CommentVo commentVo = commentService.get(id);
        return Result.success(commentVo);
    }

}
